package net.entelijan.cobean.examples.showcase.multisel;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JOptionPane;

import net.entelijan.cobean.core.ColumnDesc;
import net.entelijan.cobean.core.IMultiSelectionListCobean;
import net.entelijan.cobean.data.literature.ILitService;
import net.entelijan.cobean.data.literature.LitBook;
import net.entelijan.cobean.examples.util.ExamplesUtil;
import net.entelijan.cobean.multisel.DefaultMultiselInitializer;
import net.entelijan.scf.IShowcase;

public class OrderMultiselShowcase implements IShowcase<OrderMultiselPanel> {

	private ILitService service = null;

	public OrderMultiselShowcase() {
		super();
	}

	@Override
	public void init(final OrderMultiselPanel panel) {

		// Create an initializer
		DefaultMultiselInitializer<LitBook> ini = new DefaultMultiselInitializer<LitBook>();

		// Add the 'real' components
		ini.setCoAddButton(panel.getMultiselPanel().getAddButton());
		ini.setCoRemoveButton(panel.getMultiselPanel().getRemoveButton());
		ini.setCoLeftList(panel.getMultiselPanel().getLeftList());
		ini.setCoRightList(panel.getMultiselPanel().getRightList());

		// Define the colums that should be displayed in the lists
		ini.setColumnDescs(ColumnDesc.createPropertyOnlyColumnDescs("title",
				"subtitle", "year", "author.name"));

		final IMultiSelectionListCobean<LitBook> cobean = ini.initialize();

		// Add some data to the component bean and by that to the 'real'
		// components
		final List<LitBook> data = this.service.loadAllBooks();
		cobean.setValue(data);

		// Init the show selection button
		panel.getShowSelectionButton().addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				JOptionPane.showMessageDialog(panel, ExamplesUtil.listToString(
						cobean.getSelectedValue(), 80));
			}
		});
	}

	public ILitService getService() {
		return service;
	}

	public void setService(ILitService service) {
		this.service = service;
	}

}
